package com.yycx.module.bus.provider.handler.haiying.tasktodo.askamount;

import com.yycx.bpm.provider.handler.TaskCompleteHandler;
import com.yycx.bpm.provider.model.TaskRequest;
import com.yycx.common.base.entity.EntityMap;
import com.yycx.common.base.utils.FlymeUtils;
import com.yycx.common.security.OpenUser;
import com.yycx.module.bus.client.entity.*;
import com.yycx.module.bus.client.enums.AmountTypeEnum;
import com.yycx.module.bus.provider.service.*;
import com.yycx.module.file.provider.service.SysFileService;
import org.flowable.engine.runtime.ProcessInstance;
import org.flowable.task.api.Task;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 认款流程审批处理器
 *
 * @author zyf
 */
@Component
public class HyAskAmountTaskDataHandler implements TaskCompleteHandler {


    @Autowired
    private BusCustomService customService;
    @Autowired
    private BusDepositService depositService;
    @Autowired
    private SysFileService fileService;
    @Autowired
    private BusSalemanService salemanService;

    @Autowired
    private BusPaymentService paymentService;

    /**
     * 初始化流程变量
     *
     * @param processInstance
     * @param task
     * @param variables
     * @return
     */
    @Override
    public Map<String, Object> initTaskData(ProcessInstance processInstance, Task task, Map<String, Object> variables) {
        Map<String, Object> ret = new HashMap<>(5);
        String businessKey = processInstance.getBusinessKey();
        BusPayment payment = paymentService.getById(businessKey);
        String amountType = payment.getAmountType();
        String customId = payment.getCustomId();
        Long bankId = payment.getBankId();
        ret.put("bankId", bankId);
        List<EntityMap> fileList = fileService.selectFileListByFileType(businessKey, BusPayment.class, "");
        ret.put("uploadFile", fileList);
        //现款认款
        if (AmountTypeEnum.OrderAmount.getCode().equals(amountType)) {
            BusCustom custom = customService.getById(customId);
            ret.put("title", "现款认款单");
            ret.put("customName", payment.getPayUserName());
            if (FlymeUtils.isNotEmpty(custom)) {
                ret.put("customName", custom.getCustomName());
            }
            ret.put("returnDate", payment.getAmountDate());
            ret.put("returnAmount", payment.getAmount());
            ret.put("bankId", payment.getBankId());
        }
        //回款认款
        if (AmountTypeEnum.CustomRefund.getCode().equals(amountType)) {
            BusCustom custom = customService.getById(customId);
            ret.put("title", "回款认款单");
            if (FlymeUtils.isNotEmpty(custom)) {
                ret.put("customName", custom.getCustomName());
                ret.put("returnDate", payment.getAmountDate());
                ret.put("returnAmount", payment.getAmount());
                ret.put("bankId", payment.getBankId());
            }
        }
        //预收款认款
        if (AmountTypeEnum.YfkAmount.getCode().equals(amountType)) {
            BusCustom custom = customService.getById(customId);
            ret.put("title", payment.getTitle());
            String payUserName = payment.getPayUserName();
            if (FlymeUtils.isNotEmpty(custom)) {
                ret.put("customName", custom.getCustomName());
                if (FlymeUtils.isNotEmpty(payment.getPayUserName())) {
                    ret.put("customName", payUserName);
                }
                ret.put("returnDate", payment.getAmountDate());
                ret.put("returnAmount", payment.getAmount());
            }
            String orderId = payment.getOrderId();
            if (FlymeUtils.isNotEmpty(orderId)) {
                BusDeposit deposit = depositService.getById(orderId);
                if (FlymeUtils.isNotEmpty(deposit)) {

                }
            }
        }

        //保证金认款
        if (AmountTypeEnum.CustomBond.getCode().equals(amountType)) {
            Long saleManId = payment.getSaleManId();
            BusSaleman saleMan = salemanService.getById(saleManId);
            ret.put("title", payment.getTitle());
            String payUserName = payment.getPayUserName();
            if (FlymeUtils.isNotEmpty(saleMan)) {
                ret.put("customName", saleMan.getSaleManName());
                if (FlymeUtils.isNotEmpty(payment.getPayUserName())) {
                    ret.put("customName", payUserName);
                }
                ret.put("returnDate", payment.getAmountDate());
                ret.put("returnAmount", payment.getAmount());
            }
            String orderId = payment.getOrderId();
            if (FlymeUtils.isNotEmpty(orderId)) {
                BusDeposit deposit = depositService.getById(orderId);
                if (FlymeUtils.isNotEmpty(deposit)) {

                }
            }
        }
        //质押金认款表单
        if (AmountTypeEnum.DepositAmount.getCode().equals(amountType)) {
            Long saleManId = payment.getSaleManId();
            BusCustom custom = customService.getById(customId);
            BusSaleman saleMan = salemanService.getById(saleManId);
            ret.put("title", payment.getTitle());
            if (FlymeUtils.isNotEmpty(saleMan)) {
                ret.put("customName", saleMan.getSaleManName());
                ret.put("returnDate", payment.getAmountDate());
                ret.put("returnAmount", payment.getAmount());
            }
        }
        return ret;
    }

    /**
     * 表单提交后业务处理
     *
     * @param taskRequest
     * @param processInstance
     * @param task
     * @param completeVariables
     */
    @Override
    public void completeAfter(TaskRequest taskRequest, ProcessInstance processInstance, Task task, Map<String, Object> completeVariables, OpenUser user) {

    }

}
